<?php

namespace Modules\Activity\Entities\Jika;

use Illuminate\Database\Eloquent\Builder;
use Modules\Order\Entities\BaseOrder;

use ThrowException;

class Order extends BaseOrder
{
    // 代付订单

    protected $guarded = [];
    protected $table = 'orders';

    static public $resourceName = '集卡订单';
    static public $orderType = 'jika';

    // public $isAutoComplete = true;
    static public $isVisible = true;     // 未付款时是否可见

    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope('activityOrder', function(Builder $builder) {
            $builder->type('activity');
        });
    }

    public function generateNumber()
    {
        $number = 'J' . date('mdH') . $this->user_id . rand(10, 99);
        return $number;
    }

    // 支付完成后同步操作
    public function paidListenerSync()
    {
        // 标记为已完成
        // $this->complete();

        // 设置为可见
        // $this->update([
        //     'is_visible' => 1
        // ]);

        // // 各种活动
        // if ($this->node) {
        //     $this->node->orderPaidListenerSync($this);
        // }

        return true;
    }

    // 支付完成后同步操作
    // public function paidListenerSync()
    // {
    //     $activity = Activity::getEntity([
    //         'id' => @$this->options['activity_id']
    //     ], false);

    //     if ($activity) {
    //         $joinCode = @$this->options['join_code'];

    //         $user = $this->user;

    //         // 创建参与记录
    //         $activity->join($user, [
    //             'join_code' => $joinCode
    //         ]);
    //     }
    //     else {
    //         \Log::error('活动订单支付回调异常:' . $this->id);
    //     }

    //     return true;
    // }

}
